package vip.denglei.company.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.denglei.company.mapper.WorkWechatMsgContextMapper;
import vip.denglei.company.mapper.WorkWechatMsgInfoMapper;
import vip.denglei.company.model.WorkWechatMsgContext;
import vip.denglei.company.model.WorkWechatMsgInfo;
import vip.denglei.company.model.workwechat.AccessToken;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

public class WorkWechatSendMsgUtils {
    public static Logger logger= LoggerFactory.getLogger(WorkWechatSendMsgUtils.class);
    public static  final String Secret="IMdZZNIbvpE9p0pwlGvopjEiSLOse-ZnHOP46UW1-rM";
    public static  final String corpid="wwc846e11b754ed19a";



    //--------------------公司小程序消息推送
    public static  String getToken(){
        String url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+Secret;
        String json= HttpClientUtil.doGet(url);

        AccessToken accessToken=JsonUtils.jsonToPojo(json, AccessToken.class);
        return accessToken.getAccess_token();
    }

    /**
     *
     * @param useridList
     * @param text
     * @return
     */
    public static String sendMsg(String useridList,String text){
        JSONObject content = new JSONObject();
        JSONObject param = new JSONObject();
        content.put("content",text);
        param.put("touser",useridList);
        param.put("msgtype","text");
        param.put("agentid","1000009");
        param.put("text", content);
        logger.debug(param.toString());
        String json=HttpClientUtil.doPostJson("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+getToken(),param.toString());
        logger.debug(json);
        return "发送完成！";
    }

    //---------------------------------公司群推送
    /**
     *
     * @param msg 发送的信息
     * @param type 1、企业微信智能推送 2、智能硬件推送 3、自定义推送 4、打卡推送
     * @param id 群机器人信息主键
     * @return
     */
    public static int setBotMsg(String msg, Integer type, Integer id,WorkWechatMsgInfoMapper workWechatMsgInfoMapper,WorkWechatMsgContextMapper workWechatMsgContextMapper){
        WorkWechatMsgContext save=new WorkWechatMsgContext();
        save.setMsg(msg);
        save.setType(type);
        save.setMsgInfoId(id);
        save.setCreattime(new Date());
        WorkWechatMsgInfo workWechatMsgInfo;

        try{
            workWechatMsgInfo= workWechatMsgInfoMapper.selectByPrimaryKey(save.getMsgInfoId());
            logger.debug(workWechatMsgInfo.toString());
            if (null==workWechatMsgInfo){
                return 1;
            }
        }catch(Exception e){
            logger.debug("33333");
            return 1;
        }
        workWechatMsgContextMapper.insert(save);
        if(workWechatMsgInfo.getIsopen()!=0) {
            logger.debug("44444");
            return 2;
        }

//        String json1="{\"msgtype\": \"text\",\"text\":{\"content\":\""+save.getMsg()+"\"}}";
//        HttpClientUtil.doPostJson(workWechatMsgInfo.getWebhook(),json1);\

        String json="{\"msgtype\": \"markdown\",\"markdown\":{\"content\":\""+msg+"\"}}";
//        String json="{\"msgtype\": \"markdown\",\"markdown\":{\"content\":\""+msg+"\"}}";
        System.out.println(json);
        try {
            HttpClientUtil.doPostJson(workWechatMsgInfo.getWebhook(),new String(json.getBytes(StandardCharsets.UTF_8), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }

    public static int setBotMsg(String msg,String msgtype, Integer type, Integer id,WorkWechatMsgInfoMapper workWechatMsgInfoMapper,WorkWechatMsgContextMapper workWechatMsgContextMapper){
        WorkWechatMsgContext save=new WorkWechatMsgContext();
        save.setMsg(msg);
        save.setType(type);
        save.setMsgInfoId(id);
        save.setCreattime(new Date());
        WorkWechatMsgInfo workWechatMsgInfo;

        try{
            workWechatMsgInfo= workWechatMsgInfoMapper.selectByPrimaryKey(save.getMsgInfoId());
            logger.debug(workWechatMsgInfo.toString());
            if (null==workWechatMsgInfo){
                return 1;
            }
        }catch(Exception e){
            logger.debug("33333");
            return 1;
        }
        workWechatMsgContextMapper.insert(save);
        if(workWechatMsgInfo.getIsopen()!=0) {
            logger.debug("44444");
            return 2;
        }

//        String json1="{\"msgtype\": \"text\",\"text\":{\"content\":\""+save.getMsg()+"\"}}";
//        HttpClientUtil.doPostJson(workWechatMsgInfo.getWebhook(),json1);\

//        String json="{\"msgtype\": \"markdown\",\"markdown\":{\"content\":\""+msg+"\"}}";
        String json="{\"msgtype\": \""+msgtype+"\",\""+msgtype+"\":{\"content\":\""+msg+"\"}}";
        System.out.println(json);
        try {
            HttpClientUtil.doPostJson(workWechatMsgInfo.getWebhook(),new String(json.getBytes(StandardCharsets.UTF_8), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return 0;
    }
}
